import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# data sets
df = pd.read_csv('datasets_ML2/hour.csv')
print(df.head())

df = pd.read_csv('datasets_ML2/hour.csv', index_col='instant')
print(df.head())

df['m'] = df['dteday'].apply(lambda x: int(x.split('-')[1]))
print(df.head())

df.groupby(['season'])['cnt'].mean().plot(kind = 'bar')
plt.show()

import seaborn as sns
alist = ['temp','atemp','hum','windspeed']
sns.heatmap(df[alist].corr(), annot=True)
plt.show()

#  ==========
df1 = pd.read_csv('datasets_ML2/hour.csv')

dlist = ['dteday','instant','casual','registered']
for col in dlist:
    del df1[col]

print(df1.head())

onehotlist = ['season','yr','mnth','hr','holiday','weekday','workingday','weathersit']

df1_1 = pd.get_dummies(df1, columns=onehotlist)
print(df1_1.columns, df1_1.shape)


from sklearn.preprocessing import PolynomialFeatures
polylist = ['temp','atemp','hum','windspeed']
polyFeature = PolynomialFeatures(degree=3)
df1_2 = df1[polylist]
df1_2 = polyFeature.fit_transform(df1_2)
print(df1_2.shape)

from sklearn.preprocessing import StandardScaler
std = StandardScaler()
df1_2 = std.fit_transform(df1_2)

df1_2 = pd.DataFrame(df1_2)

for col in polylist:
    del df1_1[col]

df1_all = pd.concat([df1_1, df1_2], axis=1)
print(df1_all.head())
print("df1_all: ", df1_all.shape)

import warnings
warnings.filterwarnings('ignore')

from sklearn.model_selection import train_test_split

y = df1_all['cnt']
del df1_all['cnt']
x = df1_all

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)

from sklearn.linear_model import Lasso, Ridge
from sklearn.model_selection import GridSearchCV
l1 = Lasso()
l2 = Ridge()

param_grid = {'alpha': [0.1, 0.5, 1]}

model1 = GridSearchCV(l1, param_grid=param_grid, cv=6)
model2 = GridSearchCV(l2, param_grid=param_grid, cv=6)

model1.fit(x_train, y_train)
model2.fit(x_train, y_train)

print("model1.best_params: ", model1.best_params_)
print("model1.best_score:", model1.best_score_)

print("model2.best_params: ", model2.best_params_)
print("model2.best_score:", model2.best_score_)

model2 = Ridge(alpha=0.5)
model2.fit(x_train, y_train)

h = model2.predict(x_test)

from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
print("R2: ", r2_score(y_test, h))
print("mse: ", mean_squared_error(y_test, h))
print("mae: ", mean_absolute_error(y_test, h))
